-- --------------------------------------- --
-- 本脚本用于创建定时任务相关的菜单和权限
-- --------------------------------------- --
SET NAMES utf8mb4;
BEGIN;

-- 创建定时任务菜单目录
INSERT INTO `system_menu` (`parent_id`, `permission_id`, `name`, `type`, `uri`, `remark`, `icon_id`, `disabled`, `sort`, `created_by`, `created_at`, `updated_by`, `updated_at`, `deleted`)
VALUES (null, null, '定时任务', 'DIR', '', '', null, 0, 1, 1, CURRENT_TIMESTAMP, NULL, NULL, 0);

-- 创建任务管理菜单
INSERT INTO `system_permission` (`code`, `name`, `remark`, `created_by`, `created_at`, `updated_by`, `updated_at`, `deleted`)
VALUES ('system:job:query', '查询', '', 1, CURRENT_TIMESTAMP, NULL, NULL, 0);

SET @parent_menu_id := (SELECT MAX(`id`) FROM `system_menu`);
SET @max_permission_id := (SELECT MAX(`id`) FROM `system_permission`);
INSERT INTO `system_menu` (`parent_id`, `permission_id`, `name`, `type`, `uri`, `remark`, `icon_id`, `disabled`, `sort`, `created_by`, `created_at`, `updated_by`, `updated_at`, `deleted`)
VALUES (@parent_menu_id, @max_permission_id, '任务管理', 'SYSTEM', '/system/job', '', null, 0, 1, 1, CURRENT_TIMESTAMP, NULL, NULL, 0);

-- 创建任务管理菜单功能并分配权限
INSERT INTO `system_permission` (`code`, `name`, `remark`, `created_by`, `created_at`, `updated_by`, `updated_at`, `deleted`)
VALUES ('system:job:create', '新建', '', 1, CURRENT_TIMESTAMP, NULL, NULL, 0);

INSERT INTO `system_menu_func` (`menu_id`, `permission_id`, `name`, `remark`, `created_by`, `created_at`, `updated_by`, `updated_at`, `deleted`)
VALUES ((SELECT MAX(`id`) FROM system_menu), (SELECT MAX(`id`) FROM system_permission), '新建任务', '', 1, CURRENT_TIMESTAMP, NULL, NULL, 0);

INSERT INTO `system_permission` (`code`, `name`, `remark`, `created_by`, `created_at`, `updated_by`, `updated_at`, `deleted`)
VALUES ('system:job:delete', '删除', '', 1, CURRENT_TIMESTAMP, NULL, NULL, 0);

INSERT INTO `system_menu_func` (`menu_id`, `permission_id`, `name`, `remark`, `created_by`, `created_at`, `updated_by`, `updated_at`, `deleted`)
VALUES ((SELECT MAX(`id`) FROM system_menu), (SELECT MAX(`id`) FROM system_permission), '删除任务', '', 1, CURRENT_TIMESTAMP, NULL, NULL, 0);

INSERT INTO `system_permission` (`code`, `name`, `remark`, `created_by`, `created_at`, `updated_by`, `updated_at`, `deleted`)
VALUES ('system:job:update', '修改', '', 1, CURRENT_TIMESTAMP, NULL, NULL, 0);

INSERT INTO `system_menu_func` (`menu_id`, `permission_id`, `name`, `remark`, `created_by`, `created_at`, `updated_by`, `updated_at`, `deleted`)
VALUES ((SELECT MAX(`id`) FROM system_menu), (SELECT MAX(`id`) FROM system_permission), '修改任务', '', 1, CURRENT_TIMESTAMP, NULL, NULL, 0);

INSERT INTO `system_permission` (`code`, `name`, `remark`, `created_by`, `created_at`, `updated_by`, `updated_at`, `deleted`)
VALUES ('system:job:trigger', '执行任务', '', 1, CURRENT_TIMESTAMP, NULL, NULL, 0);

INSERT INTO `system_menu_func` (`menu_id`, `permission_id`, `name`, `remark`, `created_by`, `created_at`, `updated_by`, `updated_at`, `deleted`)
VALUES ((SELECT MAX(`id`) FROM system_menu), (SELECT MAX(`id`) FROM system_permission), '执行任务', '', 1, CURRENT_TIMESTAMP, NULL, NULL, 0);

-- 创建任务日志菜单
INSERT INTO `system_permission` (`code`, `name`, `remark`, `created_by`, `created_at`, `updated_by`, `updated_at`, `deleted`)
VALUES ('system:job:log:query', '查询', '', 1, CURRENT_TIMESTAMP, NULL, NULL, 0);

INSERT INTO `system_menu` (`parent_id`, `permission_id`, `name`, `type`, `uri`, `remark`, `icon_id`, `disabled`, `sort`, `created_by`, `created_at`, `updated_by`, `updated_at`, `deleted`)
VALUES (@parent_menu_id, (SELECT MAX(`id`) FROM system_permission), '任务日志', 'SYSTEM', '/system/job-log', '', null, 0, 1, 1, CURRENT_TIMESTAMP, NULL, NULL, 0);

-- 创建任务日志菜单功能并分配权限
INSERT INTO `system_permission` (`code`, `name`, `remark`, `created_by`, `created_at`, `updated_by`, `updated_at`, `deleted`)
VALUES ('system:job:log:delete', '删除', '', 1, CURRENT_TIMESTAMP, NULL, NULL, 0);

INSERT INTO `system_menu_func` (`menu_id`, `permission_id`, `name`, `remark`, `created_by`, `created_at`, `updated_by`, `updated_at`, `deleted`)
VALUES ((SELECT MAX(`id`) FROM system_menu), (SELECT MAX(`id`) FROM system_permission), '删除任务日志', '', 1, CURRENT_TIMESTAMP, NULL, NULL, 0);

COMMIT;
